<template>
  <div class="edit">
    <el-dialog :visible.sync="dialogVisible" center class="countDialog" :close-on-click-modal="false" @close="resetO('ruleForm')">
      <div slot="title" class="dialog-title">{{ pageTitle }}</div>
      <div>
        <el-form ref="ruleForm" :rules="rules" :model="form" size="medium" label-width="100px">
          <el-form-item prop="name" label="名称">
            <el-input v-model="form.name" placeholder="输入名称"></el-input>
          </el-form-item>
          <el-form-item prop="mid" label="所属应用">
            <el-select v-model="form.mid" :disabled="(form.id != undefined && form.mid != '' && form.mid != undefined )" placeholder="请选择所属应用">
              <el-option v-for="item in app_all" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="slot_id" label="广告位ID">
            <el-input v-model="form.slot_id" :disabled="(form.id != undefined && form.slot_id != '' && form.slot_id != undefined )" placeholder="请输入映射ID，留空由自动生成"></el-input>
          </el-form-item>
          <el-form-item prop="app_name" label="广告位名称">
            <el-input v-model="form.slot_name" :disabled="(form.id != undefined && form.slot_name != '' && form.slot_name != undefined )"></el-input>
          </el-form-item>
          <el-form-item prop="slot_type" label="广告位类型">
            <el-radio-group v-model="form.slot_type">
              <el-radio label="1">信息流</el-radio>
              <el-radio label="2">Banner</el-radio>
              <el-radio label="3">开屏</el-radio>
              <el-radio label="4">视频</el-radio>
              <el-radio label="6">插屏</el-radio>
              <el-radio label="8">贴片</el-radio>
              <el-radio label="9">激励视频</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="app_temp && app_temp.length && app_temp.length > 0" prop="template_id" label="关联模版">
            <el-select v-model="form.template_id" placeholder="请选择关联模版">
              <el-option v-for="item in app_temp" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="description" label="备注">
            <el-input type="textarea" v-model="form.description"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="onSubmit('ruleForm')">提交</el-button>
            <el-button size="small" @click="resetO('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {sspslot_add, sspslot_edit} from '@/api/sspslot'
import {GenRanSrt} from "@/utils/tools";
import {sppapp_all, sppapp_temp} from "@/api/sspapp";
export default {
  name: 'sspapp-edit',
  data(){
    return {
      dialogVisible: false,
      pageTitle:'新增',
      form:{
        media_id:''
      },
      app_all:[],
      app_temp:[],
      rules:{
        name:[
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        mid:[
          {required: true, message: '请选择所属应用', trigger: 'blur'}
        ],
        template_id:[
          {required: true, message: '请选择关联模版', trigger: 'blur'}
        ]
      }
    }
  },
  computed: {
  },
  components: {

  },
  mounted(){
  },
  created() {
  },
  methods: {
    getappall(){
      sppapp_all({}).then(res => {
        var data = res.data
        console.log(data)
        this.app_all = data
      }).catch(err => {
        console.log(err)
      })
    },
    getapptemp(){
      sppapp_temp({}).then(res => {
        var data = res.data
        console.log(data)
        this.app_temp = data
      }).catch(err => {
        console.log(err)
      })
    },
    resetO(formName){
      this.dialogVisible = false
      this.form = {
        media_id:'',
        media_type:'app',
        access:'api',
        revenue_type:'cpm'
      }
      this.$refs[formName].resetFields()
      //调用父组件方法
      this.$parent.GetList()
    },
    onSubmit(formName){
      var sdata = this.form
      sdata.flag = parseInt(sdata.flag)
      sdata.revenue_price = parseFloat(sdata.revenue_price)
      sdata.media_name = sdata.name

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.SObj(sdata).then(res => {
            var data = res.data
            console.log(data)
            this.$message({
              showClose: true,
              center:true,
              message: '操作成功',
              type: 'success'
            })
            this.dialogVisible = false
            this.$parent.GetList()
          }).catch(err => {
            console.log(err)
          })
        }else {
          return false
        }
      })

    },
    SObj(sdata){
      if (sdata.id == undefined || sdata.id == ''){
        return sspslot_add(sdata)
      }else {
        return sspslot_edit(sdata)
      }
    },
    getmid(){
      return GenRanSrt(6)
    },
    showInfo(info){
      this.dialogVisible = true
      if (info.id != undefined) {
        this.pageTitle = '编辑'
      }else {
        info.slot_type = '1'
      }
      this.form = info
      this.getappall()
      this.getapptemp()
    }
  }
}
</script>
<style lang="scss" scoped>
.edit {

}
</style>